package gob.inei.enaho2014.controller.activity;




import java.sql.SQLException;

import gob.inei.enaho.context.AppContext;
import gob.inei.enaho.controller.activity.LoginActivity;
import gob.inei.enaho.model.database.EnahoOpenHelper;
import gob.inei.enaho2014.R;
import util.android.AndroidUtil;
import util.android.sqlite.SQLiteUtil;
import util.file.FileUtil;
import util.patron.command.Command1;


import android.support.v7.app.ActionBarActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//test1();
		//test2_transaccion();
						
		crearDirectoriosIniciales();		
	
		
		AndroidUtil.abrirActivity(this, LoginActivity.class);
		this.finish();
			
	}
	
	private void crearDirectoriosIniciales() {		
		FileUtil.crearDirectorioSiNoExiste(AppContext.DIR_MARCO, true);		
		FileUtil.crearDirectorioSiNoExiste(AppContext.DIR_BACKUP, true);
		FileUtil.crearDirectorioSiNoExiste(AppContext.DIR_MARCO, true);		
	}
	
	@SuppressWarnings("unused")
	private void test1() throws SQLException {
		String sql;
		sql = "select * from Dpto";
		SQLiteUtil.executeQuery(new EnahoOpenHelper(this), sql, new Command1<Cursor>() {
			@Override
			public void execute(Cursor c) {
				System.out.println("DPTO "+c.getString(c.getColumnIndex("NombreDD")));
			}
		});
		
		sql = "select * from Odei";
		SQLiteUtil.executeQuery(new EnahoOpenHelper(this), sql, new Command1<Cursor>() {
			@Override
			public void execute(Cursor c) {
				System.out.println("ODEI "+c.getString(c.getColumnIndex("NomOdei")));
			}
		});
	}
	
	@SuppressWarnings("unused")
	private void test2_transaccion() {
		SQLiteOpenHelper h = new EnahoOpenHelper(this);
		
		try {
			SQLiteUtil.executeTransaccion(h, new Command1<SQLiteDatabase>() {			
				@Override
				public void execute(SQLiteDatabase obj) {
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0001')");
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0002')");
				}
			});		
		}
		catch(Exception ex) {
			
		}	
		
		try {
			SQLiteUtil.executeTransaccion(h, new Command1<SQLiteDatabase>() {			
				@Override
				public void execute(SQLiteDatabase obj) {
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0003')");
					obj.execSQL("INSER into Campo_Conglomerado(Conglome) values ('0004')");//error intencional
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0005')");
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0006')");
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0007')");
					obj.execSQL("Insert Into Campo_Conglomerado(Conglome) values ('0008')");
					
				}
			});
		}
		catch(Exception ex) {
			
		}
		
	}
	
}
